package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.OperationInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/13.
 */
public class OperationPresenter extends BasePresenter<OperationInterface> {
    private DataManager dataManager;
    private OperationInterface mView;
    private Subscription mSubscription;

    public OperationPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(OperationInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }


    /**
     * @param type          操作类型
     * @param id            id
     * @param appraiseback  退回原因
     * @param appraiseprice 评估价格
     * @param appraisedes   评估说明
     */
    public void OperationData(final String type, String id, String appraiseback, String appraiseprice, String appraisedes, String userid) {
        mSubscription = dataManager.OperationData(type, id, appraiseback, appraiseprice, appraisedes, userid).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.newThread()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null && mView != null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData successData) {
                mView.showresult(successData);
            }
        });
    }

    /**
     * 线上退回 审核驳回
     *
     * @param type
     * @param userid
     * @param B2CPrice     售车价
     * @param C2BPrice     收车价
     * @param appraiseback 退回原因  审核驳回原因
     */
    public void OperationBack(final String type, String userid, String B2CPrice, String C2BPrice, String appraiseback, String taskid) {
        mSubscription = dataManager.OperationBack(type, userid, B2CPrice, C2BPrice, appraiseback, taskid).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.newThread()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData successData) {
                mView.showresult(successData);
            }
        });
    }


}
